 ##
 # Copyright 2008 Friendster Inc Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
 # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 
 # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 # See the License for the specific language governing permissions and limitations under the License.
 ##

require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
require "action_controller/test_process"
require "action_controller/integration"

# helpers for all Friendster unit tests
class Test::Unit::TestCase
  
  def assert_friendster_overrides_method(object, methodSymbol)
    # by convention, Friendster uses the alias_method_chain on certain methods
    friendsterMethodSymbol = "#{methodSymbol}__FRIENDSTER".to_sym
    aliasedMethodSymbol = "#{methodSymbol}__ALIASED".to_sym
    # string description of this object
    objectDescription = object.is_a?(Class) ? object.to_s : object.class.to_s
    
    # ensure that the original method is still available
    assert object.respond_to?(aliasedMethodSymbol, true), "Could not find original #{objectDescription}::#{methodSymbol}"
    
    # ensure that the object has the Friendster override
    assert object.respond_to?(friendsterMethodSymbol, true), "Could not find Friendster override of #{objectDescription}::#{methodSymbol}"
    
    # ensure that the override is actually overriding the given method
    assert object.method(methodSymbol) == object.method(friendsterMethodSymbol), "#{objectDescription}::#{methodSymbol} does not appear to be overridden by Friendster"
  end
  
end

# dummy controller used in many test cases
class DummyController < ActionController::Base
 
  before_filter :require_friendster_login, :only => [:index]
  
  # actions
  def index
    render :text => "viewing index"
  end
    
  def nofilter
    render :text => "no filter needed"
  end
  
  def shouldbeinstalled
    if require_friendster_install
      render :text => "app is installed"
    end
  end
  
  def doredirect
    redirect_to params[:redirect_url]
  end
  
  def render_foobar_action_on_callback
    render :text => url_for("#{friendster_callback_path}foobar")
  end
  
  
  # utility methods
  
  def rescue_action(e) 
    raise e 
  end
  
  def init_friendster
     fs=friendster_session_holder
     fs.logdin_user="231555"
     fs.session_key="122412352532363246.54656"
  end
    
  def simulate_friendster(moreParams={})
    self.init_friendster
    @extra_params = moreParams
  end
  
  def params
    if @extra_params
      (super || {}).merge(@extra_params)
    else
      super
    end
  end
  
  # for external apps
  def finish_friendster_login
    render :text => "finished friendster login"
  end
  
end

# dummy model used in a few test cases
class DummyModel < ActiveRecord::Base
  acts_as_friendster_user
  
  def friendster_uid
    "dummyuid"
  end
  
  def friendster_session_key
    "dummysessionkey"
  end
end

